//
// Copyright 2006 Brian Watkins
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//

DragArrange = function( id, sGroup, config ) {
  this.dragArrangeInit( id, sGroup, config );
};

YAHOO.extend( DragArrange, YAHOO.util.DDProxy );

DragArrange.prototype.dragArrangeInit = function( id, sGroup, config ) {
  if ( !id ) { return; }

  this.init( id, sGroup, config );
  this.initFrame();

  this.shimInnerHTML;
  this.shimClass;
  this.shimElement;

  this.dragArea;
  this.removed = false;
};

DragArrange.prototype.startDrag = function( x, y ) {
  var el = this.getEl();
  this.shimInnerHTML = el.innerHTML;
  this.shimClass = el.className;

  el.innerHTML = '';
  el.className = 'dragShim';
  el.style.background = cat_bgcolor;

  this.ncats = document.getElementById( "ncats" );

  this.shimElement = el;

  //need to do this everytime we drag in case
  //the size of the list has changed due to additions or
  //deletions
  var cats = this.getOrderedElements();
  var topRegion = YAHOO.util.DDM.getLocation( YAHOO.util.DDM.getDDById( cats[0].id ) );
  var bottomRegion = YAHOO.util.DDM.getLocation( YAHOO.util.DDM.getDDById( cats[ cats.length - 1 ].id ) );

  this.dragArea = new YAHOO.util.Region( topRegion.top, topRegion.right, bottomRegion.bottom, bottomRegion.left );

};

DragArrange.prototype.onDragEnter = function( e, id ) {
  if ( id == 'addCategory' ) { return; }

  var el = YAHOO.util.DDM.getElement( id );

  var dind = this.findRealIndex( el );

  if ( !this.removed ) {
    this.ncats.removeChild( this.shimElement );
  }

  //I know it looks bad to get the ordered elements again
  //because dind is w/r/t the ordered elements before we
  //removed the shimelement. But this makes everything
  //work perfectly. if we are dragging up the list then
  //the index will be the same. if we are dragging down it is
  //index + 1 which gets us the action we want. 
  var els = this.getOrderedElements();
  this.ncats.insertBefore( this.shimElement, els[ dind ] );

  this.removed = false;
  
  YAHOO.util.DDM.refreshCache( this.groups );
};

DragArrange.prototype.onDrag = function( e ) {
  var dragPoint = new YAHOO.util.Point( YAHOO.util.Event.getPageX( e ), 
                                        YAHOO.util.Event.getPageY( e ) );
  if ( !this.removed && !this.dragArea.contains( dragPoint ) ) {
    this.ncats.removeChild( this.shimElement );
    this.removed = true;
    YAHOO.util.DDM.refreshCache( this.groups );
  }
};

DragArrange.prototype.onInvalidDrop = function( e ) {
  //note since we are always moving the dragged element
  //if you drop it within the list you will drop it on itself
  //which fires an invalid drop event instead of the normal
  //drag drop event. so check to see if we are in the drag area
  //or not to determine whether we are moving or deleting
  var dropPoint = new YAHOO.util.Point( YAHOO.util.Event.getPageX( e ), 
                                        YAHOO.util.Event.getPageY( e ) );
  if ( this.dragArea.contains( dropPoint ) ) {
    var pos = this.findRealIndex( this.getEl() );
    //alert( "moving " + this.id + " to: " + pos );
    this.moveElement( pos );
  } else {
    //alert( "deleting: " + this.id );
    this.deleteElement();
  }
};

DragArrange.prototype.endDrag = function( e, id ) {
  this.shimElement.innerHTML = this.shimInnerHTML;
  this.shimElement.className = this.shimClass;
  if ( this.shimClass == 'nsSelected' ) {
    this.shimElement.style.background = cat_hicolor;
    var cvs = this.shimElement.getElementsByTagName( 'CANVAS' );
    drawDown( cvs.item( 0 ).id, false );
  }  
  else { 
    this.shimElement.style.background = cat_medcolor;
  }
};

DragArrange.prototype.onMouseDown = function( e ) {
  dragging = true;
};

DragArrange.prototype.onMouseUp = function( e ) {
  dragging = false;
};

DragArrange.prototype.findRealIndex = function( dd ) {
  var els = this.getOrderedElements();
  for ( var i = 0; i < els.length; i++ ) {
    if ( els[i].id == dd.id ) {
      return i;
    }
  }
  return -1;
}

DragArrange.prototype.getOrderedElements = function () {
  var els = [];
  var grp = this.ncats.getElementsByTagName( "DIV" );
  var x = 0;
  for ( var i = 0; i < grp.length; i++ ) {
    var el = grp.item( i );
    if ( YAHOO.util.DDM.isDragDrop( el.getAttribute( "id" ) ) ) {
      els[ x++ ] = el;
    }
  }
  return els;
};

DragArrange.prototype.moveElement = function( pos ) {
  //if we were doing this nicely then implementations
  //could just extend this class and implement these methods.

  var id = this.getEl().getAttribute( "newsCategoryId" );
  var surl = 'updateNews?action=moveCat&categoryId=' + id + '&pos=' + pos;
  
  var callback =
  { 
    success : this.handleSuccess,
    failure : this.handleFailure,
    argument : { action : 'move' }
  };

  YAHOO.util.Connect.asyncRequest( 'GET', surl, callback );
};

DragArrange.prototype.deleteElement = function( pos ) {
  //if we were doing this nicely then implementations
  //could just extend this class and implement these methods.

  var id = this.getEl().getAttribute( "newsCategoryId" );
  var surl = 'updateNews?action=deleteCat&categoryId=' + id;
  
  var callback =
  { 
    success : this.handleDeleteSuccess,
    failure : this.handleFailure,
    argument : { action : 'delete',
                 elementId : this.id }
  };

  YAHOO.util.Connect.asyncRequest( 'GET', surl, callback );
};

DragArrange.prototype.handleDeleteSuccess = function( obj ) {  
  if ( obj.argument.elementId == currentFeed.getAttribute( "id" ) ) {
    var nfw = document.getElementById( "newsFeedWindow" );
    nfw.src = "newsFeed.jsp";
    currentFeed = '';
    currentSrcName = '';
    currentNSS = '';
  }
};

DragArrange.prototype.handleSuccess = function( obj ) {
  //nothing
};

DragArrange.prototype.handleFailure = function( obj ) {
  alert( "Failure for " + obj.argument.action + " action!" );
};